#include "AbDiopleReverseTask.h"

LineAbReverseTask::LineAbReverseTask()
{
	X = 10;
	Y = 10;
	Length = 1;
	CacheLNum = 0;
	CachedCurveIndex = -1;
}


//---------------------------------------------------------------------------------------------------------
void LineAbReverseTask::AfterInit()
{
	Times.Allocate(OriginalCurves[0].GetSize());
	BufVector.Allocate(OriginalCurves[0].GetSize());
	for(int i=0; i<CurvesCount; i++)
	{
		CachedCurve[i].Allocate(OriginalCurves[0].GetSize());
	}
	
	Dipole.SetLength(Length);

	//fill times
	for(int i=0; i<OriginalCurves[0].GetSize(); i++)
	{
		Times[i] = OriginalCurves[0][i].X;
	}
}


//---------------------------------------------------------------------------------------------------------
void LineAbReverseTask::CalcAbnormalCurve(int iIndex, ConductiveLayer *iNewMedium, int iLayersCount)
{
	//check if need update cached curve
	bool recalc_cache = false;
	if(CacheLNum == 0 || CacheLNum != iLayersCount || CachedCurveIndex != iIndex)
		recalc_cache = true;
	else
	{
		for(int i=0; i<CacheLNum; i++)
		{
			if(CachedL[i].Sigma != OldMedium[i].Sigma || CachedL[i].H != OldMedium[i].H)
			{
				recalc_cache = true;
				CachedL[i] = OldMedium[i];
			}
		}
	}
	if(recalc_cache)
	{
		Dipole.SetMedium(OldMedium, LayersNum);
		if(iIndex == 0)
		{
			Dipole.Ex(X, Y, 0, CachedCurve[iIndex].Data, Times.Data, Times.GetSize());
		}
		else if(iIndex == 1)
		{
			Dipole.DBzDt(X, Y, 0, CachedCurve[iIndex].Data, Times.Data, Times.GetSize());
		}
		else if(iIndex == 2)
		{
			Dipole.Ey(X, Y, 0, CachedCurve[iIndex].Data, Times.Data, Times.GetSize());
		}
		CachedCurveIndex = iIndex;
	}
	

	//calculate abnormal curve
	Dipole.SetMedium(iNewMedium, iLayersCount);
	if(iIndex == 0)
	{
		Dipole.Ex(X, Y, 0, BufVector.Data, Times.Data, Times.GetSize());
	}
	else if(iIndex == 1)
	{
		Dipole.DBzDt(X, Y, 0, BufVector.Data, Times.Data, Times.GetSize());
	}
	else if(iIndex == 2)
	{
		Dipole.Ey(X, Y, 0, BufVector.Data, Times.Data, Times.GetSize());
	}


	//fill actual curve
	for(int i=0; i<BufVector.GetSize(); i++)
	{
		AbnomalCurves[iIndex][i].Y = (BufVector[i] - CachedCurve[iIndex][i]);
		ActualCurves[iIndex][i].Y = BufVector[i];
	}
}


//---------------------------------------------------------------------------------------------------------
void LineAbReverseTask::CalcActualCurve(int iIndex)
{
	Dipole.SetMedium(Medium, LayersNum);
	if(iIndex == 0)
	{
		Dipole.Ex(X, Y, 0, BufVector.Data, Times.Data, Times.GetSize());
	}
	else if(iIndex == 1)
	{
		Dipole.DBzDt(X, Y, 0, BufVector.Data, Times.Data, Times.GetSize());
	}
	else if(iIndex == 2)
	{
		Dipole.Ey(X, Y, 0, BufVector.Data, Times.Data, Times.GetSize());
	}
	

	//copy medium	
	CacheLNum = LayersNum;
	for(int i=0; i<CacheLNum; i++)
	{
		CachedL[i] = OldMedium[i];
	}


	//fill actual curve
	for(int i=0; i<BufVector.GetSize(); i++)
	{
		ActualCurves[iIndex][i].Y = BufVector[i];	
		CachedCurve[iIndex][i] = ActualCurves[iIndex][i].Y;
	}
	CachedCurveIndex = iIndex;
	ActualCurves[iIndex].Filter();
}


//---------------------------------------------------------------------------------------------------------
LineAbReverseTask::~LineAbReverseTask()
{
}
